//
//  CYHUD.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/10.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//
#define HUD_alpha 0.6
#import "CYHUD.h"
@interface CYHUD(){
    CYHUDBlock _myblock;
}
@end
@implementation CYHUD

- (instancetype)initWithView:(UIView *)view text:(NSString *)text HUDmode:(MBProgressHUDMode)mode complete:(CYHUDBlock)complete{
    self = [super initWithView:view];
    if (self) {
        _myblock = complete;
        [view addSubview:self];
        self.label.text = text;
        self.square = YES;//强制背景框高宽相等
        self.mode = mode;//进度条样式
        self.animationType = MBProgressHUDAnimationZoomOut;//显示和隐藏的动画类型
        self.removeFromSuperViewOnHide = YES;//隐藏时，从父视图上删除
        [self showAnimated:YES];
        WS(self)
        self.completionBlock = ^{
            if (complete)
                complete(weakSelf.label.text);
        };
    }
    return self;
}

+ (CYHUD *)getHud{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window makeKeyAndVisible];
    
    //初始化菊花
    CYHUD *hud = [[CYHUD alloc]init];
    [window addSubview:hud];
    
    hud.animationType = MBProgressHUDAnimationZoomOut;//显示和隐藏的动画类型
    hud.removeFromSuperViewOnHide = YES;
    hud.label.numberOfLines = 0;
    hud.label.font = [UIFont systemFontOfSize:15 weight:UIFontWeightLight];
    hud.center = window.center;
    //    hud.completionBlock = ^{
    //        hud = nil;
    //    };
    return hud;
}

+ (CYHUD *)hud{
    CYHUD  * hud = [CYHUD getHud];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.animationType = MBProgressHUDAnimationZoomOut;//显示和隐藏的动画类型
    hud.removeFromSuperViewOnHide = YES;//隐藏时，从父视图上删除
    hud.userInteractionEnabled = NO;//菊花旋转时，可点击
    return hud;
}

+ (void)show:(NSString *)message{
    CYHUD *hud = [CYHUD getHud];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = message;
    [hud showAnimated:YES];
    [hud hideAnimated:YES afterDelay:2];
}

- (void)show:(NSString *)message complete:(CYHUDBlock)complete{
    self.mode = MBProgressHUDModeText;
    self.userInteractionEnabled = NO;//菊花旋转时，可点击
    self.label.text = message;
    [self showAnimated:YES];
    [self hideAnimated:YES afterDelay:2];
    self.completionBlock = ^{
        if (complete)
            complete(nil);
    };
}

- (void)hidden:(BOOL)animation complete:(CYHUDBlock)complete{
    [super hideAnimated:YES];
    if (complete)
        complete(nil);
    
}

- (void)hidden:(BOOL)animation text:(NSString *)text complete:(CYHUDBlock)complete{
    self.label.text = text;
    [super hideAnimated:YES];
    if (complete)
        complete(nil);
    
}

- (void)addToWindow{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window makeKeyAndVisible];
    [window addSubview:self];
}

@end
